﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace wspol_zad5
{
    public enum State
    {
        FREE,
        RENT,
        MODIFY
    };

    /*public struct BookData
    {
        int id;
        State state;
    }*/

    public class Resource
    {
        private Dictionary<int, State> resource;
        private bool isBook;
        private int count;
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        public Resource(int cnt, bool isBook)
        {
            count = cnt;
            this.isBook = isBook;
            resource = new Dictionary<int, State>();

            for (int i = 0; i < cnt; i++)
                resource.Add(i, State.FREE);

            /*resource.Add("Władca Pierścieni", new BookData() { 0, State.FREE });
            resource.Add("Wichrowe Wzgórza", new BookData() { 0, State.FREE });
            resource.Add("Rok 1984", new BookData() { 0, State.FREE });
            resource.Add("Hobbit", new BookData() { 0, State.FREE });
            resource.Add("Buszujący w zbożu", new BookData() { 0, State.FREE });
            resource.Add("Przeminęło z wiatrem", new BookData() { 0, State.FREE });*/
        }

        public bool RentBook(int i)
        {
            if (!isBook) return true;

            bool succes = false;

            lock (resource)
            {
                if (resource[i] == State.RENT)
                    succes = false;
                else
                {
                    resource[i] = State.RENT;
                    succes = true;
                }
            }

            return succes;
        }

        public bool ReturnBook(int i)
        {
            if (!isBook) return true;

            bool succes = false;

            lock (resource)
            {
                if (resource[i] == State.FREE)
                    succes = false;
                else
                {
                    resource[i] = State.FREE;
                    succes = true;
                }
            }

            return succes;
        }

        public bool StartModyfiBook(int i)
        {
            bool succes = false;

            lock (resource)
            {
                if (resource[i] == State.MODIFY)
                    succes = false;
                else
                {
                    resource[i] = State.MODIFY;
                    succes = true;
                }
            }

            return succes;
        }

        public bool EndModyfiBook(int i)
        {
            bool succes = false;

            lock (resource)
            {
                if (resource[i] == State.FREE)
                    succes = false;
                else
                {
                    resource[i] = State.FREE;
                    succes = true;
                }
            }

            return succes;
        }
    }

}
